﻿@{
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    List<WeChatParking.Entity.Role> PermissionList = ViewBag.PermissionList;
}
<div class="">
    <div class="ibox-content">
        <div class="jqGrid_wrapper">
            <div class="col-sm-12 jqGrid-toolbar">
                <form class="form-inline search-form" id="search-form">
                    <div class="form-group">
                        <label for="exampleInputEmail2">用户代码</label>
                        <input type="text" class="form-control" id="search-UserCode" name="UserCode">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail2">用户名</label>
                        <input type="text" class="form-control" id="search-UserName" name="UserName">
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="glyphicon glyphicon-search"></i>查询
                    </button>
                </form>
                <div class="btn-group " role="group">
                    <button class="btn btn-primary" id="btn-add">
                        <i class="glyphicon glyphicon-plus"></i>新增
                    </button>
                    <button class="btn btn-success" id="btn-edit">
                        <i class="glyphicon glyphicon-pencil"></i>编辑
                    </button>
                    <button class="btn btn-danger" id="btn-del">
                        <i class="glyphicon glyphicon-trash"></i>删除
                    </button>
                </div>
            </div>
            <table id="grid"></table>
            <div id="grid_pager"></div>
        </div>
    </div>
</div>

<div class="modal fade" id="form-modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">角色信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" method="post" action="/Admin/SaveUserInfo">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">用户名：</label>
                            <div class="col-sm-9">
                                <input type="text" name="UserName" id="userName" class="form-control" placeholder="请输入用户名" required maxlength="50">
                                <input type="hidden" id="UserId" name="UserId" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">用户代码：</label>
                            <div class="col-sm-9">
                                <input type="text" id="userCode" name="UserCode" class="form-control" placeholder="请输入用户代码" required maxlength="50" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">联系方式：</label>
                            <div class="col-sm-9">
                                <input type="text" name="UserPhone" class="form-control" placeholder="请输入联系方式" required maxlength="50">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">用户角色：</label>
                            <div class="col-sm-9">
                                <select  class="form-control" name="RoleID">
                                    @foreach (var item in PermissionList)
                                    {
                                         <option value="@item.ID">@item.RoleName</option>
                                    }
                                   
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="btn-save">保存</button>
            </div>
        </div>
    </div>
</div>

<div id="form-config-dialog" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>

@section BodyFooter{
    <script>
        $(document).ready(function () {
            $("#grid").jqGrid({
                url: '/Admin/UserInfoQuery',
                colNames: ["ID", "用户名", "用户代码", "联系方式", "企业名称","角色代码", "角色名称", "创建时间", "创建人", "更新时间", "更新人"],
                colModel: [{
                    name: "UserId",
                    index: "UserId",
                    width: 60,
                    key: true,
                    sorttype: "int"
                },
                {
                    name: "UserName",
                    index: "UserName",
                    sortable: false,
                    width: 90
                },
                {
                    name: "UserCode",
                    index: "UserCode",
                    sortable: false,
                    width: 100
                },
                {
                    name: "UserPhone",
                    index: "UserPhone",
                    width: 120,
                },
                {
                    name: "Enterprise_Name",
                    index: "Enterprise_Name"
                },
                {
                    name: "RoleID",
                    index: "RoleID",
                    width: 0
                },
                {
                    name: "RoleName",
                    index: "RoleName",
                },
                {
                    name: "create_time",
                    index: "create_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                },
                {
                    name: "create_user",
                    index: "create_user"
                },
                {
                    name: "update_time",
                    index: "update_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                },
                {
                    name: "update_user",
                    index: "update_user",
                    sortable: false
                }],
                caption: "角色信息"
            });
            $("#grid").adaptGridSizeInWindow();
        });

        $('#btn-add').click(function () {
            
            document.getElementById("UserId").value = 0;
            document.getElementById("userName").disabled = false;
            document.getElementById("userCode").disabled = false;
            $('#form-modal').toggleModal(true, ' 新 增 ');
            $('#form-modal form').bindData(null);
            
        });

        $('#btn-edit').click(function () {
            var row = $("#grid").getGridSelectedData();
            if (!row)
                return;
            document.getElementById("userName").disabled = true;
            document.getElementById("userCode").disabled = true;
            $('#form-modal form').bindData(row);
            $('#form-modal').toggleModal(true, ' 编 辑 ');
            

        });

        $('#btn-del').click(function () {
            var ids = $("#grid").jqGrid('getGridParam', 'selarrrow');
            if (ids.length == 0)
                return;
            $.confirm('确认删除数据？', function () {
                $.postJsonAjax('/Admin/DeleteUserInfo', { ids: ids.join(',') }, function (data) {
                    $("#grid").trigger("reloadGrid");
                    if (data.Code == 1) {
                        toastr.info('数据删除成功');
                    } else {
                        toastr.error(data.Message);
                    }

                });
            });
        });
        $.validator.setDefaults({
            submitHandler: function (form) {
                $(form).ajaxSubmit(function (d) {
                    $("#grid").trigger("reloadGrid");
                    $('#form-modal').toggleModal(false);
                    toastr.info('数据保存成功');
                });
            }
        });

        $('#form-modal form').validate()

        $('#btn-save').click(function (form) {
            $('#form-modal form').submit();
        });

        $('#search-form').submit(function () {
            var data = $(this).serializeArray();
            var postData = {};
            $.each(data, function (i, n) {
                postData[n.name] = n.value;
            });

            $("#grid").jqGrid('setGridParam', {
                postData: postData, //发送数据 
                page: 1
            }).trigger("reloadGrid"); //重新载入 
            return false;
        });


    </script>
}

